// preload.js

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('api', {
    // --- 从渲染器到主进程 (调用) ---
    connect: (address) => ipcRenderer.send('connect', address),
    disconnect: () => ipcRenderer.send('disconnect'),
    sendData: (data) => ipcRenderer.send('send-data', data),
    loadProtoDir: () => ipcRenderer.invoke('load-proto-dir'),
    loadProtoFile: (fileName) => ipcRenderer.invoke('load-proto-file', fileName),
    getMessageDetails: (messageType) => ipcRenderer.invoke('get-message-details', messageType),
    loadCodecScript: () => ipcRenderer.invoke('load-codec-script'),
    decodeProto: (data) => ipcRenderer.invoke('decode-proto', data),
    setReceiveMessageType: (messageType) => ipcRenderer.invoke('set-receive-message-type', messageType),
    // 添加新的业务脚本加载方法
    loadBusinessScriptFile: () => ipcRenderer.invoke('load-business-script-file'),
    // 删除原有的方法：
    // loadBusinessScriptDir: () => ipcRenderer.invoke('load-business-script-dir'),
    // loadBusinessScript: (fileName) => ipcRenderer.invoke('load-business-script', fileName),
    // --- 从主进程到渲染器 (监听) ---
    onLog: (callback) => ipcRenderer.on('log', (_event, value) => callback(value)),
    onDataReceived: (callback) => ipcRenderer.on('data-received', (_event, data, protocol) => callback(data, protocol)),
    onConnectionStatus: (callback) => ipcRenderer.on('connection-status', (_event, value) => callback(value)),
    autoLoadProtoDir: (dirPath) => ipcRenderer.invoke('auto-load-proto-dir', dirPath),
    autoLoadCodecScript: (scriptPath) => ipcRenderer.invoke('auto-load-codec-script', scriptPath),
    autoLoadBusinessScript: (scriptPath) => ipcRenderer.invoke('auto-load-business-script', scriptPath),
    // --- 清理监听器 ---
    removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel),
});
